package org.standard.web.action;

import java.util.List;

import org.standard.base.model.BaseRole;
import org.standard.base.model.BaseUser;
import org.standard.commons.service.Service;
import org.standard.commons.struts2.BaseAction;
import org.standard.commons.utils.Constants;
import org.standard.commons.utils.FlipPageInfo;
import org.standard.notice.model.Notice;
import org.standard.web.model.LevelMessage;

import com.googlecode.jsonplugin.annotations.JSON;

public class WebAction extends BaseAction {

	private LevelMessage message;
	private Service service;
	private FlipPageInfo flipPageInfo = new FlipPageInfo();
	private String s_name;
	private String s_phone;
	private String s_theme;
	private String s_cardNo;
	private Notice notice;
	
	private BaseUser user;
	private String userLoginString;
	
	public String index() {
		
		return CREATE;
	}
	
	public String create() {
		
		message.setStatus("0");
		service.createPOJO(message);
		return search();
	}

	public String search() {
		
		String [][]search = {
				{"name", s_name},
				{"phone", s_phone},
				{"theme", s_theme},
				{"cardNo", s_cardNo},
				{"status", "1"},
		};
		String [][]sort = {
				{"date", "1"},
		};
		flipPageInfo = service.findPOJOList(LevelMessage.class, flipPageInfo , search, sort);
		return LIST;
	}
	public String searchAdmin() {
		
		String [][]search = {
				{"name", s_name},
				{"phone", s_phone},
				{"theme", s_theme},
				{"cardNo", s_cardNo},
		};
		String [][]sort = {
				{"date", "1"},
		};
		flipPageInfo = service.findPOJOList(LevelMessage.class, flipPageInfo , search, sort);
		return LIST;
	}
	public String modifyPre() {
		
		message = (LevelMessage) service.findPOJOById(LevelMessage.class, message.getId());
		return MODIFY;
	}
	public String detail() {
		
		modifyPre();
		return DETAIL;
	}
	public String modify() {
		
		message.setStatus("1");
		BaseRole role = (BaseRole) session.get(Constants.SESSION_ROLE);
		if (role.getRoleId().equals(Constants.ROLE_STAFF)) {
			BaseUser user = (BaseUser) session.get(Constants.SESSION_USER);
			message.setAduitName(user.getUserName());
		}
		service.modifyPOJO(message);
		return search();
	}
	public String findNoticeJson(){
		
		String [][]search = {
		};
		String [][]sort = {
				{"date", "1"},
		};
		flipPageInfo = service.findPOJOList(Notice.class, flipPageInfo , search, sort);
		notice = new Notice();
		if (flipPageInfo != null) {
			List list = flipPageInfo.getData();
			if (list != null && !list.isEmpty()) {
				notice = (Notice) list.get(0);
			}
		}
		return JSON;
	}
	
	public String findUser(){
		
		if (user == null || user.getUserName() == null || "".equals(user.getUserName())) {
			userLoginString = "姓名不能为空";
			return JSON;
		}
		List list = service.findPOJOList("from BaseUser where userName = '"+user.getUserName()+"'");
		StringBuffer sb = new StringBuffer();
		if (list != null && !list.isEmpty()) {
			for (int i = 0; i < list.size(); i++) {
				user = (BaseUser) list.get(i);
				sb.append(user.getUserLogin());
				if (i != list.size() - 1) {
					sb.append(",");
				}
			}
			userLoginString = sb.toString();
		} else {
			userLoginString = "查询无记录";
		}
		return JSON;
	}
	public LevelMessage getMessage() {
		return message;
	}

	public void setMessage(LevelMessage message) {
		this.message = message;
	}

	public void setService(Service service) {
		this.service = service;
	}

	public FlipPageInfo getFlipPageInfo() {
		return flipPageInfo;
	}

	public void setFlipPageInfo(FlipPageInfo flipPageInfo) {
		this.flipPageInfo = flipPageInfo;
	}

	public String getS_name() {
		return s_name;
	}

	public void setS_name(String s_name) {
		this.s_name = s_name;
	}

	public String getS_phone() {
		return s_phone;
	}

	public void setS_phone(String s_phone) {
		this.s_phone = s_phone;
	}

	public String getS_theme() {
		return s_theme;
	}

	public void setS_theme(String s_theme) {
		this.s_theme = s_theme;
	}

	public String getS_cardNo() {
		return s_cardNo;
	}

	public void setS_cardNo(String no) {
		s_cardNo = no;
	}
	@JSON(name="notice")
	public Notice getNotice() {
		return notice;
	}

	public void setNotice(Notice notice) {
		this.notice = notice;
	}

	public BaseUser getUser() {
		return user;
	}

	public void setUser(BaseUser user) {
		this.user = user;
	}

	@JSON(name="userLoginString")
	public String getUserLoginString() {
		return userLoginString;
	}

	public void setUserLoginString(String userLoginString) {
		this.userLoginString = userLoginString;
	}
	
	
}
